﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Account_kcgl_kcgl : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<proc_GetHaveAllotExamInfoResult> ExamRes;
            ExamRes = (from p in db.proc_GetHaveAllotExamInfo()
                       //       where p.subjectNo.ToString().Contains(subjectNo) && p.classNo.Contains(classNo)
                       select p).ToList();
            DropDownList2.DataSource = ExamRes;
            DropDownList2.DataTextField = "classNo";
            DropDownList2.DataValueField = "classNo";
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "-请选择-");
            BindData();
        }
    }
    private void BindData()
    {
        List<proc_GetHaveAllotExamInfoResult> ExamRes;
        ExamRes = (from p in db.proc_GetHaveAllotExamInfo()
                   //       where p.subjectNo.ToString().Contains(subjectNo) && p.classNo.Contains(classNo)
                   select p).ToList();
        GridView2.DataSource = ExamRes;
        GridView2.DataBind();
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            int subjectNo = Convert.ToInt32(GridView2.Rows[i].Cells[1].Text);
            string classNo = (GridView2.Rows[i].Cells[2].FindControl("HyperLink1") as HyperLink).Text;
            //Response.Write(classNo);
            var getsubnum = (from g in db.proc_GetSubjectBySubjectNoClassNo(subjectNo, classNo)
                             select g).ToList();
            //string subNum = getsubnum.subNum;
            /*var ProRes = (from p in db.proc_GetProfessionBySubjectNoClassNo(subjectNo, classNo, subNum)
                      select p).ToList();*/
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("subjectNo"));
            dt.Columns.Add(new DataColumn("classNo"));
            dt.Columns.Add(new DataColumn("subNum"));
            dt.Columns.Add(new DataColumn("subName"));
            dt.Columns.Add(new DataColumn("proName"));
            dt.Columns.Add(new DataColumn("realNum"));

            foreach (proc_GetSubjectBySubjectNoClassNoResult item in getsubnum)
            {

                var ProRes = (from p in db.proc_GetProfessionBySubjectNoClassNo(subjectNo, classNo, item.subNum)
                              select p).ToList();
                foreach (proc_GetProfessionBySubjectNoClassNoResult items in ProRes)
                {
                    DataRow dr = dt.NewRow();
                    dr["subjectNo"] = subjectNo;
                    dr["classNo"] = classNo;
                    dr["subNum"] = item.subNum;
                    dr["subName"] = item.subName;
                    dr["proName"] = items.proName;
                    dr["realNum"] = items.realNum;
                    dt.Rows.Add(dr);
                }

            }

            System.Web.UI.WebControls.DataList DL1 =
                (System.Web.UI.WebControls.DataList)GridView2.Rows[i].FindControl("DataList1");
            System.Web.UI.WebControls.DataList DL2 =
                (System.Web.UI.WebControls.DataList)GridView2.Rows[i].FindControl("DataList2");
            System.Web.UI.WebControls.DataList DL3 =
                (System.Web.UI.WebControls.DataList)GridView2.Rows[i].FindControl("DataList3");
            DL1.DataSource = dt;
            DL1.DataBind();
            DL2.DataSource = dt;
            DL2.DataBind();
            DL3.DataSource = dt;
            DL3.DataBind();
        }
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lb1 = (Label)e.Row.Cells[0].FindControl("Label1");
            lb1.Text = (GridView2.PageIndex * GridView2.PageSize + e.Row.RowIndex + 1).ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string subjectNo1 = DropDownList1.SelectedValue;
        int? subjectNo = null;
        string classNo = DropDownList2.SelectedValue;
        List<proc_GetHaveAllotExamInfoResult> ExamRes;
        //Response.Write(subjectNo1);
        if (subjectNo1 == "-请选择-" || classNo == "-请选择-")
        {
            if (subjectNo1 != "-请选择-")
            {
                subjectNo = Convert.ToInt32(DropDownList1.SelectedValue);
            }
            ExamRes = (from p in db.proc_GetHaveAllotExamInfo()
                       where p.subjectNo.ToString().Contains(subjectNo1) || p.classNo.Contains(classNo)
                       select p).ToList();
        }
        else
        {
            subjectNo = Convert.ToInt32(DropDownList1.SelectedValue);
            ExamRes = (from p in db.proc_GetHaveAllotExamInfo()
                       where p.subjectNo.ToString().Contains(subjectNo1) && p.classNo.Contains(classNo)
                       select p).ToList();
        }
        GridView2.DataSource = ExamRes;
        GridView2.DataBind();
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            var getsubnum = (from g in db.proc_GetSubjectBySubjectNoClassNo(subjectNo, classNo)
                             select g).ToList();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("subjectNo"));
            dt.Columns.Add(new DataColumn("classNo"));
            dt.Columns.Add(new DataColumn("subNum"));
            dt.Columns.Add(new DataColumn("subName"));
            dt.Columns.Add(new DataColumn("proName"));
            dt.Columns.Add(new DataColumn("realNum"));

            foreach (proc_GetSubjectBySubjectNoClassNoResult item in getsubnum)
            {

                var ProRes = (from p in db.proc_GetProfessionBySubjectNoClassNo(subjectNo, classNo, item.subNum)
                              select p).ToList();
                foreach (proc_GetProfessionBySubjectNoClassNoResult items in ProRes)
                {
                    DataRow dr = dt.NewRow();
                    dr["subjectNo"] = subjectNo;
                    dr["classNo"] = classNo;
                    dr["subNum"] = item.subNum;
                    dr["subName"] = item.subName;
                    dr["proName"] = items.proName;
                    dr["realNum"] = items.realNum;
                    dt.Rows.Add(dr);
                }

            }

            System.Web.UI.WebControls.DataList DL1 =
                (System.Web.UI.WebControls.DataList)GridView2.Rows[i].FindControl("DataList1");
            System.Web.UI.WebControls.DataList DL2 =
                (System.Web.UI.WebControls.DataList)GridView2.Rows[i].FindControl("DataList2");
            System.Web.UI.WebControls.DataList DL3 =
                (System.Web.UI.WebControls.DataList)GridView2.Rows[i].FindControl("DataList3");
            DL1.DataSource = dt;
            DL1.DataBind();
            DL2.DataSource = dt;
            DL2.DataBind();
            DL3.DataSource = dt;
            DL3.DataBind();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string style = @"<style> .text { mso-number-format:\@; } </script> ";
        Response.ClearContent();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView2.RenderControl(htw);
        Response.Write(style);
        Response.Write(sw.ToString());
        Response.End();
        //导出后恢复分页
        GridView2.AllowPaging = true;
        //重新绑定
        BindData();
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 得到该控件
        GridView theGrid = sender as GridView;
        int newPageIndex = 0;
        if (e.NewPageIndex == -3)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = theGrid.BottomPagerRow;

            if (pagerRow != null)
            {
                //得到text控件
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (txtNewPageIndex != null)
            {
                //得到索引
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            //点击了其他的按钮
            newPageIndex = e.NewPageIndex;
        }
        //防止新索引溢出
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        //得到新的值
        theGrid.PageIndex = newPageIndex;
        BindData();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
}